package com.heima.media.config;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @BelongsProject: heima-leadnews
 * @BelongsPackage: com.heima.media.config
 * @Author: wangjian
 * @CreateTime: 2022-07-02  19:07
 * @Description: TODO
 * @Version: 1.0
 */

public class TaskPoolConfig {
    @Bean("asyncExecutor")
    public Executor asyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程池数
        executor.setCorePoolSize(4);
        //最大线程池数
        executor.setMaxPoolSize(20);
        //任务队列的容量
        executor.setQueueCapacity(200);
        // 非核心线程的存活时间
        executor.setKeepAliveSeconds(60);
        // 线程池的前缀名称
        executor.setThreadNamePrefix("asyncExecutor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        return executor;
    }
}
